package com.mohe.zhihui.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mohe.zhihui.dao.IDao;
import com.mohe.zhihui.model.Project;
import com.mohe.zhihui.service.interfaces.IProject;
import com.mohe.zhihui.utils.Page;

public class ProjectImpl implements IProject {
	
	private IDao<Project> projectDao;

	public IDao<Project> getProjectDao() {
		return projectDao;
	}

	public void setProjectDao(IDao<Project> projectDao) {
		this.projectDao = projectDao;
	}

	@Override
	public List<Project> getProjectList(Project prj, Page page) {
		StringBuffer where = new StringBuffer();
		where.append("from Project where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(!StringUtils.isBlank(prj.getTitle())){
			where.append(" and title like ?");
			params.add("%%"+prj.getTitle()+"%%");
		}
		where.append(" order by createDate desc");
		List<Project> list = projectDao.query(where.toString(), (page.getCurrPage()-1)*page.getPageSize(), page.getPageSize(), params);
		if(list!=null && list.size()>0){
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(Project prj) {
		StringBuffer where = new StringBuffer();
		where.append("from Project where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(!StringUtils.isBlank(prj.getTitle())){
			where.append(" and title like ?");
			params.add("%%" + prj.getTitle() + "%%");
		}
		return projectDao.getCount(where.toString(), params);
	}

	@Override
	public boolean saveProject(Project prj) {
		return projectDao.insert(prj);
	}

	@Override
	public Project getProjectById(Long timeKey) {
		Project prj = projectDao.getByKey(timeKey);
		if(prj != null){
			return prj;
		}
		return null;
	}

	@Override
	public boolean updateProject(Project prj) {
		return projectDao.update(prj);
	}

	@Override
	public boolean delProject(Project prj) {
		return projectDao.delete(prj.getTimeKey());
	}

	@Override
	public List<Project> getAllProject() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return projectDao.query(where.toString(), null);
	}

}
